import { _decorator, Component, director, Label, find, ProgressBar } from 'cc';
import { Constant } from '../Common/Constant';
import { UIManager } from '../Manager/UIManager';
const { ccclass, property } = _decorator;

@ccclass('LoadingPanel')
export class LoadingPanel extends Component {

    progressBar: ProgressBar = null!;
    progressLb: Label = null!;

    start() {
        this.progressBar = find("LoadingBar/ProgressBar", this.node).getComponent(ProgressBar);
        this.progressLb = find("LoadingBar/ProgressLabel", this.node).getComponent(Label);
    }

    Show(sceneName: string) {
        director.preloadScene(sceneName, (completedCount: number, totalCount: number, item: any) => {
            var progress = completedCount / totalCount;
            this.progressBar.progress = progress;
            this.progressLb.string = `正在加载：${(progress * 100).toFixed()}%`;
        }, () => {
            director.loadScene(sceneName, () => {
                UIManager.Instance.HidePanel(Constant.Panel.LoadingPanel);
            });
        });

    }

}